package com.ntd.frame.awt.component;

import ntd.util.UtilThreadPool;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

public class NtdJFrame extends JFrame {

    private String title;
    private int width;
    private int height;
    private JLabel backJLabel;

    public NtdJFrame(String title, Integer width, Integer height) {
        this.title = title;
        this.width = width;
        this.height = height;
        this.init();
    }

    public NtdJFrame() {
        this("窗口标题", 500, 500);
    }

    private void init() {
        this.setTitle(title);
        this.setSize(width, height);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());

        JPanel jPanel = (JPanel) this.getContentPane();
        jPanel.setOpaque(false);

        backJLabel = new JLabel();
        backJLabel.setBounds(0, 0, width, height);

        this.getLayeredPane().add(backJLabel, Integer.valueOf(Integer.MIN_VALUE));

        this.setLocationRelativeTo(null);

        this.setVisible(true);

        UtilThreadPool.scheduledThreadPool.scheduleAtFixedRate(() -> {
            backJLabel.setSize(this.getSize());
        }, 0L, 50L, TimeUnit.MILLISECONDS);
    }

    public void setBackImg(BufferedImage bufferedImage) {
        ImageIcon imageIcon = new ImageIcon(bufferedImage);

        imageIcon.setImage(imageIcon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT));

        backJLabel.setIcon(imageIcon);
    }
}
